package com.kzd.gtboon.main.home

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.gtboon.R
import com.kzd.gtboon.custom.setTransitionStatusT
import com.kzd.gtboon.databinding.ActivityHomeDoingsBinding
import com.kzd.gtboon.entity.Doings
import com.kzd.gtboon.entity.DoingsTop
import com.kzd.gtboon.entity.GameDoing
import com.kzd.gtboon.jetpack.vm.HomeViewModel
import com.kzd.gtboon.main.KEY_DOINGS_DOING
import com.kzd.gtboon.main.KEY_DOINGS_TITLE
import com.kzd.gtboon.main.KEY_DOINGS_TYPE
import com.kzd.gtboon.main.game.adapter.Game1Adapter
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.image.loadImage
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel

class HomeDoingsActivity : OwnerViewBindingActivity<ActivityHomeDoingsBinding>() {

    private val mDoingTitle: String? by lazy {
        intent?.extras?.getString(KEY_DOINGS_TITLE)
    }

    private val mDoingMark: String? by lazy {
        intent?.extras?.getString(KEY_DOINGS_TYPE)
    }

    private val mDoingDoing: Doings<GameDoing>? by lazy {
        intent?.extras?.getSerializable(KEY_DOINGS_DOING) as? Doings<GameDoing>
    }

    private var mStart: Long = 0

    private val mGame1Adapter: Game1Adapter<GameDoing> by lazy {
        Game1Adapter()
    }

    private val mViewModel: HomeViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityHomeDoingsBinding
        get() = ActivityHomeDoingsBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.flHomeDoingsBar
            barColor = R.color.white
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

        mViewBinding.flHomeDoingsBar.setTitleTextContent(mDoingTitle)

        mViewBinding.includeHoneDoingsBody.rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
        }.adapter = mGame1Adapter

        mViewBinding.includeHoneDoingsBody.mSmartRefreshLayout.setOnRefreshListener {
            val layout = mDoingMark
                ?: return@setOnRefreshListener
            mViewModel.homeDoings(layout)
        }
        mViewBinding.includeHoneDoingsBody.mSmartRefreshLayout.setOnLoadMoreListener {
            val layout = mDoingMark
                ?: return@setOnLoadMoreListener
            mViewModel.homeDoings(layout, mStart, 2, (true))
        }
    }

    override fun initData() {
        if (mDoingDoing == null) mDoingMark?.let {
            mViewModel.homeDoings(it)
        } else mDoingDoing?.let {
            fillData(it)
        }
    }

    override fun initViewMode() {
        mViewModel.homeDoingsLiveData.observe(this as LifecycleOwner, { response ->
            setTransitionStatusT(response,
                mGame1Adapter,
                mViewBinding.includeHoneDoingsBody.mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            mStart = data.start
            if (isLoadMore) {
                mGame1Adapter.addData(data.list)
            } else {
                fillData(data)
            }
        })
    }

    private fun fillData(doing: Doings<GameDoing>) {
        mGame1Adapter.setHeaderView(createHeaderView(doing.top))
        mViewBinding.clHomeDoingsBack.setBackgroundColor(Color.parseColor(doing.top.color))
        mGame1Adapter.setNewInstance(doing.list)
        mStart = doing.start
    }

    private fun createHeaderView(top: DoingsTop): View {
        val view = LayoutInflater.from(this as Context)
            .inflate(R.layout.item_doings_header, null)
        view.findViewById<TextView>(R.id.tvDoingsHeaderContent)
            .text = top.content
        view.findViewById<ImageView>(R.id.ivDoingsHeaderImage)
            .loadImage(top.image)
        return view
    }

}